<template>
    <div>
        <el-row>
            <el-col :span="24">
                <el-form ref="form" :rules="rules" :model="form" label-width="100px">
                    <el-form-item label="事件标题" prop="title">
                        <el-input v-model="form.title" placeholder=""/>
                    </el-form-item>
                    <el-form-item label="事件内容" prop="content">
                        <el-input type="textarea" v-model="form.content" :autosize="{ minRows: 15,}" placeholder=""/>
                    </el-form-item>
                    <el-form-item label="事件时间" prop="time">
                        <el-date-picker
                            v-model="form.time"
                            type="datetime"
                            placeholder="选择日期时间"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            default-time="12:00:00">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" :loading="loading" @click="submit">确定</el-button>
                        <el-button @click="cancel">返回</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    export default {
        props: {
            data: {

            }
        },
        data() {
            return {
                loading: false,
                form: {},
                formDefault: {
                    title: '',
                    content: '',
                    time: '',
                },
                rules: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' },
                    ],
                    content: [
                        { required: true, message: '请输入详细内容', trigger: 'blur' },
                    ],
                    time: [
                        { required: true, message: '请选择事件时间', trigger: 'blur' },
                    ],
                },
                options: this.$store.state.appNews.options,
            }
        },
        computed: {

        },
        watch: {
            data (newVal) {
                this.syncForm();
            }
        },
        methods: {
            getFormAction() {
                return this.form.id ? 'appNews/Update' : 'appNews/Store';
            },
            submit() {
                this.$refs.form.validate(valid => {
                    if (!valid) return false;
                    this.loading = true;
                    this.$store.dispatch(this.getFormAction(), this.form).then(res => {
                        this.$message.success(res.data.message);
                        this.$emit('submit');
                    }).finally(() => {
                        this.loading = false;
                    });
                });
            },
            cancel() {
                this.$emit('cancel');
            },
            syncForm() {
                this.form = Object.assign({}, this.data.id ? this.data : this.formDefault);
                if (!this.form.time) this.form.time = moment().format('yyyy-MM-DD HH:mm:ss');
            }
        },
        created () {
            this.syncForm();
        }
    }
</script>

<style lang="scss" scoped>
.item-row {
    margin: 0 0 10px 0;
}
</style>

